#!/usr/bin/env python
# encoding: utf-8


"""
@file: qiujiexishi.py
@time: 2017/4/26 下午6:45
"""
# 使用换元法 求解析式
from mathsolver.functions.base import *
from sympy import sin, cos, pi
from mathsolver.functions.sanjiao import sanjiao_utils as su
from mathsolver.functions.budengshi import common_opers as co


# 求解析式
# style Input paramer1: sinFunc(已知的复合三角函数); paramer2: sinFunc(目标函数)
# 若f(sinx)=2-cos2x,则f(cosx)等于()
class QiuSanJiaoHanShuJieXiShi(BaseFunction):
    def solver(self, *args):
        self.label.add('三角函数换元法求解析式')
        arg1 = sympify('sin(x)')  # 原函数的中的f(arg1)
        expr1 = sympify('2 - cos(2*x)')  # 元函数的解析式f(arg1) = expr1
        arg2 = sympify('cos(x)')  # 目标函数的f(arg2)
        _t = sympify('t')
        _x = sympify('x')
        t1_coef, t1_type, t1_arg, t1_const = su.simp_trig_info(arg1)
        t2_coef, t2_type, t2_arg, t2_const = su.simp_trig_info(arg2)
        _x_v = co.isolve_eq2(BaseEq([_t, t1_arg]))[0]
        self.steps.append(['令' + BaseEq([_t, t1_arg]).printing(), ';' + BaseEq([_x, _x_v]).printing()])
        expr1_sub = expr1.subs(_x, _x_v)
        self.steps.append(['得:', new_latex(expr1_sub)])
        if t1_coef == 1 and t2_coef == 1 and t1_type == sin and t2_type == cos:
            t2_arg = t2_arg + pi / 2
        elif t1_coef == 1 and t2_coef == 1 and t1_type == cos and t2_type == sin:
            t2_arg = t2_arg - pi / 2
        last_expr = expr1_sub.subs(_t, t2_arg)
        self.steps.append(['所以求得:', new_latex(last_expr)])
        self.output.append(BasePoly(last_expr))
        return self


if __name__ == '__main__':
    pass
